package com.yjy.mybase.http;

import com.google.gson.JsonSyntaxException;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.qpg.superhttp.mode.ApiCode;
import com.qpg.superhttp.utils.GsonUtil;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * author : YangJinyang
 * create : 2019-10-29 16:26
 * description :网络请求封装类
 */
public class SuperHttpHelper<T> {

    public static final String BaseUrl = "http://q3je3v.natappfree.cc/gaming/";

    public static final String NewsList = "gaming/selectList";//新闻列表

    public static final String DetailInfo = "gaming/detail";//详情

    private static SuperHttpHelper superHttpHelper;


    public static SuperHttpHelper superHttpHelper() {
        if (superHttpHelper == null) {
            synchronized (SuperHttpHelper.class) {
                if (superHttpHelper == null) {
                    superHttpHelper = new SuperHttpHelper();
                }
            }
        }
        return superHttpHelper;
    }

    public void get(String url, Map<String, String> map, final ICallback<T> iCallback) {
        SuperHttp.get(url)
                .addParams(map)
                .request(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String json) {
                        try {
                            if (getType(iCallback).equals(String.class)) {
                                iCallback.onSuccess((T) json);
                            } else {
                                Type type = getType(iCallback);
                                iCallback.onSuccess((T) GsonUtil.gson().fromJson(json, type));
                            }
                        } catch (JsonSyntaxException e) {
                            System.out.println("qiao--->" + e.getMessage());
                            iCallback.onFail(ApiCode.Request.PARSE_ERROR, e.getMessage());
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        iCallback.onFail(errCode, errMsg);
                    }
                });
    }

    public void post(String url, HashMap<String, String> map, final ICallback<T> iCallback) {
        SuperHttp.post(url)
                .addParams(map)
                .request(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String json) {
                        try {
                            if (getType(iCallback).equals(String.class)) {
                                iCallback.onSuccess((T) json);
                            } else {
                                Type type = getType(iCallback);
                                iCallback.onSuccess((T) GsonUtil.gson().fromJson(json, type));
                            }
                        } catch (JsonSyntaxException e) {
                            System.out.println("qiao--->" + e.getMessage());
                            iCallback.onFail(ApiCode.Request.PARSE_ERROR, e.getMessage());
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        iCallback.onFail(errCode, errMsg);
                    }
                });
    }

    protected <T> Type getType(T t) {
        Type genType = t.getClass().getGenericSuperclass();//获得带有泛型的父类
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();//获取参数化类型的数组，泛型可能有多个
        Type type = params[0];
        Type finalNeedType;
        if (params.length > 1) {
            if (!(type instanceof ParameterizedType)) {
                throw new IllegalStateException("没有填写泛型参数");
            }
            finalNeedType = ((ParameterizedType) type).getActualTypeArguments()[0];
        } else {
            finalNeedType = type;
        }
        return finalNeedType;
    }
}
